package com.duoec.magichour.mapper;

import com.duoec.magichour.core.BaseMapper;
import com.duoec.magichour.pojo.Message;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface MessageMapper extends BaseMapper<Message, Long> {
    /**
     * 通过消息IDs，获取对应的消息
     *
     * @param messageIds 消息IDs
     * @return 消息列表
     */
    @Select({"<script>",
            "SELECT id,message_status,receiver_id",
            "FROM message",
            "WHERE id IN",
            "<foreach item='item' collection='list'",
            "open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<Message> selectByIds(@Param("list") List<Long> messageIds);

    /**
     * 修改某此消息的状态
     *
     * @param messageIds    消息IDs
     * @param messageStatus 消息状态
     * @return 更新数据数量
     */
    @Update({"<script>",
            "UPDATE message",
            "SET message_status=#{messageStatus}, update_time=now()",
            "WHERE id IN",
            "<foreach item='item' collection='messageIds'",
            "open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    int updateMessageStatus(
            @Param("messageIds") List<Long> messageIds,
            @Param("messageStatus") int messageStatus
    );

    /**
     * 获取某个接收者的某个状态消息列表
     *
     * @param userId        接收者ID
     * @param messageStatus 消息状态
     * @param skip          跳过{skip}条记录
     * @param limit         最多返回{limit}条记录
     * @return 消息列表
     */
    @Select("SELECT * FROM message WHERE receiver_id=#{userId} AND message_status=#{messageStatus} LIMIT #{skip}, #{limit}")
    List<Message> listMessage(
            @Param("userId") long userId,
            @Param("messageStatus") int messageStatus,
            @Param("skip") int skip,
            @Param("limit") int limit
    );
}